﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using CMN.Controllers;
using CMN.Controllers.Filters;
using CMN.Common;
using System.Text;
using CMN.UI;
using CMN.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Config;
namespace CMN.Controllers
{
    [UserAuthorize]
    [LoggerFilter()]
    [ExceptionFilter()]
    public class XFSearchController : Controller
    {

        public string Index(string searchWords, int pageSize, int pageIndex)
        {

            DateTime dt = DateTime.Now;
            PageHelperEntity pageHelper = new PageHelperEntity();
            QueryParam qp=new QueryParam();
            if(pageSize>1000){
                pageSize=1000;
            }
            if (pageIndex < 0)
            {
                pageIndex = 1;
            }
            if (pageIndex > 100)
            {
                pageIndex = 100;
            }
            
            qp.PageSize=pageSize;
            qp.PageIndex=pageIndex;
            //qp.Where=searchWords;
            qp.PageIndex = pageIndex;
            qp.PageSize = pageSize;

            qp.Where = searchWords;
            List<Module_ListInfo> list = new List<Module_ListInfo>();
            if (string.IsNullOrEmpty(searchWords))
            {
                
            }
            else
            {
                LuceneHelper lucene = new LuceneHelper();

                list = lucene.Search(qp, "", 100, 1);

            }
           
            StringBuilder sb = new StringBuilder();
            sb.Append("{");
            sb.Append(string.Concat("Date:\"", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "\","));
            sb.Append("Version:\"1.0\",");
            sb.Append("SourceSiteName:\"生活百事通\",");
            sb.Append(string.Concat("TotalCount:\"",qp.Count.ToString(),"\","));
            sb.Append("Items:[");

            SiteConfigInfo siteInfo = SiteConfigs.GetSiteConfig();
            Normal_ChannelEntity channel2 = null;
            
            foreach (Module_ListInfo listInfo in list)
            {
                channel2 = new Normal_ChannelEntity();
                channel2 = MvcWebCacheManager.GetNormal_ChannelByPath(listInfo.Path);
                if (channel2 != null)
                {
                    listInfo.Url = string.Concat(channel2.DirectoryUrl, "/", (string.IsNullOrEmpty(siteInfo.ChannelUrl) ? listInfo.Id.ToString() : string.Format(siteInfo.ChannelUrl, listInfo.Id.ToString())));
                }
            }
           
            foreach (Module_ListInfo listInfo in list)
            {
                
                sb.Append("{");
                sb.Append(string.Concat("Title:\"", listInfo.Title.Replace('"',' '), "\","));
                sb.Append(string.Concat("Summary:\"", listInfo.Summary.Replace('"', ' '), "\","));
                sb.Append(string.Concat("Category:\"",MvcWebCacheManager.GetNormal_Channel(listInfo.ChannelId).Title, "\","));
                sb.Append(string.Concat("AddDate:\"", listInfo.AddDate.ToString("yyyy-MM-dd"), "\","));
                sb.Append(string.Concat("WapUrl:\"",siteInfo.Url.Replace("www.","m."), listInfo.Url, "\","));
                sb.Append(string.Concat("WebUrl:\"",siteInfo.Url, listInfo.Url, "\""));
                sb.Append("},");
            }
            sb.Remove(sb.ToString().Length - 1, 1);
            sb.Append("]");
            sb.Append("}");



            return sb.ToString();
        }
        
    }
}
